home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / nstext.zip / NS_TEXT.PAS < prev    next >
Pascal/Delphi Source File  |  1991-12-05  |  38KB  |  895 lines

  1. {$I-,F+}
  2.  
  3. Unit NS_Text;                                                          { v0.6 }
  4. {*****************************************************************************}
  5. { Contains all elements to manipulate text files.                             }
  6. { Author: Michael Weber [71301,1221]                                          }
  7. { Revision history:                                                           }
  8. {    1) 20 Nov 91 first version released (v0.5)                               }
  9. {    2)  1 Dec 91 version 0.6 released                                        }
  10. {       * Added methods SetFAttr & GetFAttr to manipulate file attributes     }
  11. {       * Added methods SetFTime & GetFTime to manipulate file time stamps    }
  12. {       * Added ability to copy file within its instance ( i.e. no longer     }
  13. {         need to destruct instance, copy file, & recreate instance)          }
  14. {       * Added ability to instantate leaving file closed (via NS_Closed      }
  15. {         enumerated type)                                                    }
  16. {       * Added method FileSplit to return subcomponents of the file name     }
  17. {       * Methods made recursive where possible to reduce code / increase     }
  18. {         integrity                                                           }
  19. {       * Private method CClose added to increase integrity                   }
  20. {                                                                             }
  21. { FileOpenType defines the state to which TextFile is opened as follows:      }
  22. {    NS_Reset   - Open existing file for input                                }
  23. {    NS_ReWrite - Create new file for output                                  }
  24. {    NS_Append  - Open existing file for output to append                     }
  25. {    NS_Closed  - Leaves the file closed                                      }
  26. {                                                                             }
  27. { The following are personal reminders for future versions. I invite you,     }
  28. { though, to extend NS_Text to address these needs or add new features.       }
  29. { - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - }
  30. { Idea*** Keep current record pointer in read(ln) & write(ln).                }
  31. {         Create Seek Procedure to properly place next read(ln) / write(ln)   }
  32. { Idea*** Parm (in Init) for text buffer size ability to chg. size at runtime }
  33. { Idea*** Have linked list of open files.                                     }
  34. { Idea*** Have dialog window to open file... mabey in decendent object...     }
  35. { Question*** Should I allow multiple instances for same file?                }
  36. {*****************************************************************************}
  37.  
  38. Interface {===================================================================}
  39.    Const
  40.       TextOk          : Boolean = True;
  41.       TextError       : Byte = 0;
  42.  
  43.    Type
  44.       FileNameType    = PChar;
  45.       FileIntegType   = (NS_Normal, NS_High);
  46.       FileOpenType    = (NS_Reset, NS_ReWrite, NS_Append, NS_Closed);
  47.  
  48.       PTextFile    = ^TTextFile;
  49.       TTextFile     = Object
  50.          FlushBuffer  : Boolean;   { Set to TRUE when record buffered to disk }
  51.          FileName     : FileNameType;
  52.          FileInteg    : FileIntegType;
  53.          F            : Text;
  54.          FileMode     : FileOpenType;     { Reset / ReWrite / Append / Closed }
  55. {         TextBuff     : Array[0..1023] Of Char;            { Buffer for file }
  56.  
  57.          Constructor  Init(Name:FileNameType; State:FileOpenType);
  58.          Procedure    Integrity(Integ:FileIntegType);
  59.          Procedure    Reset;
  60.          Procedure    ReWrite;
  61.          Procedure    Append;
  62.          Procedure    Flush; Virtual;
  63.          Procedure    SetFlush;
  64.          Procedure    Rename(NewName:FileNameType); Virtual;
  65.          Procedure    FCopy(DestName:FileNameType); Virtual;
  66.          Function     EOF : Boolean; Virtual;
  67.          Function     EOLN : Boolean; Virtual;
  68.          Function     SeekEOF : Boolean; Virtual;
  69.          Function     SeekEOLN : Boolean; Virtual;
  70.          Procedure    Read(VAR Item:String); Virtual;
  71.          Procedure    ReadLn(VAR Item:String); Virtual;
  72.          Procedure    Write(Item:String); Virtual;
  73.          Procedure    WriteLn(Item:String); Virtual;
  74.          Procedure    GetFTime(VAR Time:LongInt); Virtual;
  75.          Procedure    SetFTime(VAR Time:LongInt); Virtual;
  76.          Function     FileSplit(Dir, Name, Ext:PChar) : Word; Virtual;
  77.          Procedure    GetFAttr(VAR Attr:Word); Virtual;
  78.          Procedure    SetFAttr(VAR Attr:Word); Virtual;
  79.          Destructor   Close; Virtual;
  80.          Destructor   Erase; Virtual;
  81.       Private
  82.          Procedure    CClose;
  83.       End;{Object}
  84.  
  85. Implementation {==============================================================}
  86.    Uses
  87.       WinDOS;                                              { Borland DOS unit }
  88.  
  89.    Constructor TTextFile.Init;
  90.    {--------------------------------------------------------------------------}
  91.    { Extensible? NO - Constructors are not extensible                         }
  92.    {                                                                          }
  93.    { Error information:                                                       }
  94.    {    None - no system procedures called                                    }
  95.    {                                                                          }
  96.    { First we allocate 80 bytes of heap space for the qualified file name.    }
  97.    { Instead of calling the system procedures Reset, ReWrite, and Append, we  }
  98.    { call the objects methods for consistancy of error handling and           }
  99.    { protocol.                                                                }
  100.    {--------------------------------------------------------------------------}
  101.       Begin
  102.          GetMem(FileName, fsPathName+1);                  { Allow room for #0 }
  103.  
  104.          FileExpand(FileName, Name);
  105.          FileMode := State;
  106.  
  107.          Assign(F, FileName);
  108.  
  109. {         SetTextBuf(F, TextBuff);      }
  110.  
  111.          Case FileMode Of
  112.             NS_Reset : Begin
  113.                Reset;
  114.             End; {Reset}
  115.  
  116.             NS_ReWrite : Begin
  117.                ReWrite;
  118.             End; {ReWrite}
  119.  
  120.             NS_Append : Begin
  121.                Append;
  122.             End; {Append}
  123.          End; {Case}
  124.       End;
  125.    {EndConstructor}
  126.  
  127.    Procedure TTextFile.Integrity;
  128.    {--------------------------------------------------------------------------}
  129.    { Extensible? NO - only purpose of method is to set Integrity level        }
  130.    {--------------------------------------------------------------------------}
  131.       Begin
  132.          FileInteg := Integ;
  133.       End;
  134.    {EndProcedure}
  135.  
  136.    Procedure TTextFile.Reset;
  137.    {--------------------------------------------------------------------------}
  138.    { Error information:                                                       }
  139.    {    Reset   :   2 File not found                                          }
  140.    {                3 Path not found                                          }
  141.    {                4 Too many open files                                     }
  142.    {                5 File access denied                                      }
  143.    {              102 File not assigned                                       }
  144.    {--------------------------------------------------------------------------}
  145.       Begin
  146.          FlushBuffer := False;                    { Initialize flag for Flush }
  147.          FileMode := NS_Reset;
  148.          System.Reset(F);
  149.  
  150.          Case IOResult Of
  151.             2 :  Begin                                       { File not found }
  152.                System.ReWrite(F);
  153.                Reset;
  154.             End; {2}
  155.  
  156.             3 : Begin                                        { Path not found }
  157.                TextOK := False;
  158.                TextError := 3;
  159.             End; {3}
  160.  
  161.             4 : Begin                                   { Too many open files }
  162.                TextOK := False;
  163.                TextError := 4;
  164.             End; {4}
  165.  
  166.             5 : Begin                                    { File access denied }
  167.                TextOK := False;
  168.                TextError := 5;
  169.             End; {5}
  170.  
  171.             102 : Begin                                   { File not assigned }
  172.                TextOK := False;                   { This should never happen! }
  173.                TextError := 102;
  174.             End; {102}
  175.          End; {Case}
  176.       End;
  177.    {EndProcedure}
  178.  
  179.    Procedure TTextFile.ReWrite;
  180.    {--------------------------------------------------------------------------}
  181.    { Error information:                                                       }
  182.    {    ReWrite :   3 Path not found                                          }
  183.    {                4 Too many open files                                     }
  184.    {                5 File access denied                                      }
  185.    {              102 File not assigned                                       }
  186.    {--------------------------------------------------------------------------}
  187.       Begin
  188.          FlushBuffer := False;                    { Initialize flag for Flush }
  189.          FileMode := NS_ReWrite;
  190.          System.ReWrite(F);
  191.  
  192.          Case IOResult Of
  193.             3 : Begin                                        { Path not found }
  194.                TextOK := False;
  195.                TextError := 3;
  196.             End; {3}
  197.  
  198.             4 : Begin                                   { Too many open files }
  199.                TextOK := False;
  200.                TextError := 4;
  201.             End; {4}
  202.  
  203.             5 : Begin                                    { File access denied }
  204.                TextOK := False;
  205.                TextError := 5;
  206.             End; {5}
  207.  
  208.             102 : Begin                                   { File not assigned }
  209.                TextOK := False;                   { This should never happen! }
  210.                TextError := 102;
  211.             End; {102}
  212.          End; {Case}
  213.       End;
  214.    {EndProcedure}
  215.  
  216.    Procedure TTextFile.Append;
  217.    {--------------------------------------------------------------------------}
  218.    { Error information:                                                       }
  219.    {    Append  :   2 File not found                                          }
  220.    {                3 Path not found                                          }
  221.    {                4 Too many open files                                     }
  222.    {                5 File access denied                                      }
  223.    {              102 File not assigned                                       }
  224.    {--------------------------------------------------------------------------}
  225.       Begin
  226.          FlushBuffer := False;                    { Initialize flag for Flush }
  227.          FileMode := NS_Append;
  228.          System.Append(F);
  229.  
  230.          Case IOResult Of
  231.             2 : System.ReWrite(F);                           { File Not Found }
  232.  
  233.             3 : Begin                                        { Path not found }
  234.                TextOK := False;
  235.                TextError := 3;
  236.             End; {3}
  237.  
  238.             4 : Begin                                   { Too many open files }
  239.                TextOK := False;
  240.                TextError := 4;
  241.             End; {4}
  242.  
  243.             5 : Begin                                    { File access denied }
  244.                TextOK := False;
  245.                TextError := 5;
  246.             End; {5}
  247.  
  248.             102 : Begin                                   { File not assigned }
  249.                TextOK := False;                   { This should never happen! }
  250.                TextError := 102;
  251.             End; {102}
  252.          End; {Case}
  253.       End;
  254.    {EndProcedure}
  255.  
  256.    Procedure TTextFile.Flush;
  257.    {--------------------------------------------------------------------------}
  258.    { Error information:                                                       }
  259.    {    Flush   : 101 Disk write error                                        }
  260.    {              103 File not open                                           }
  261.    {--------------------------------------------------------------------------}
  262.       Begin
  263.          If (FlushBuffer) Then
  264.             Begin
  265.                System.Flush(F);
  266.  
  267.                Case IOResult Of
  268.                   101 : Begin                              { Disk write error }
  269.                      TextOK := False;
  270.                      TextError := 101;
  271.                   End; {101}
  272.  
  273.                   103 : Begin                                 { File not open }
  274.                      TextOK := False;             { This should never happen! }
  275.                      TextError := 103;
  276.                   End; {3}
  277.                Else
  278.                   FlushBuffer := False;                          { Clear Flag }
  279.                End; {Case}
  280.             End
  281.          {EndIf}
  282.       End;
  283.    {EndProcedure}
  284.  
  285.    Procedure TTextFile.SetFlush;
  286.    {--------------------------------------------------------------------------}
  287.    { Extensible? NO - only purpose of method is to set FlushBuffer = True     }
  288.    {--------------------------------------------------------------------------}
  289.       Begin
  290.          FlushBuffer := True;
  291.       End;
  292.    {EndProcedure}
  293.  
  294.    Procedure TTextFile.Rename;
  295.    {--------------------------------------------------------------------------}
  296.    { Error information:                                                       }
  297.    {    Close   : 101 Disk write error                                        }
  298.    {              103 File not open                                           }
  299.    {    ReName  :   2 File not found                                          }
  300.    {                3 Path not found                                          }
  301.    {                5 File access denied                                      }
  302.    {               17 Cannot rename across drives                             }
  303.    {              102 File not assigned                                       }
  304.    {--------------------------------------------------------------------------}
  305.       Begin
  306.          CClose;                 { ReName must NEVER be used on an open file! }
  307.  
  308.          If (TextOK) Then
  309.             Begin
  310.                FileExpand(FileName, NewName);
  311.                System.Rename(F, FileName);
  312.                Case IOResult Of
  313.                   2 : Begin                                  { File not found }
  314.                      TextOK := False;
  315.                      TextError := 2;
  316.                   End; {2}
  317.  
  318.                   3 : Begin                                  { Path not found }
  319.                      TextOK := False;
  320.                      TextError := 3;
  321.                   End; {3}
  322.  
  323.                   5 : Begin                               {File access denied }
  324.                      TextOK := False;
  325.                      TextError := 5;
  326.                   End; {5}
  327.  
  328.                   17 : Begin                    { Cannot rename across drives }
  329.                      TextOK := False;
  330.                      TextError := 17;
  331.                   End; {17}
  332.  
  333.                   102 : Begin                             { File not assigned }
  334.                      TextOK := False;             { This should never happen! }
  335.                      TextError := 102;
  336.                   End; {102}
  337.                Else            { After ReName, open file for processing again }
  338.                   Begin
  339.                      Case FileMode Of
  340.                         NS_Reset   : Reset;
  341.                         NS_ReWrite : ReWrite;
  342.                         NS_Append  : Append;
  343.                      End; {Case}
  344.                   End;
  345.                End; {Case}
  346.             End;
  347.          {EndIf}
  348.       End;
  349.    {EndProcedure}
  350.  
  351.    Procedure TTextFile.FCopy;
  352.    {--------------------------------------------------------------------------}
  353.    { Error information:                                                       }
  354.    {    Reset   :   2 File not found                                          }
  355.    {                3 Path not found                                          }
  356.    {                4 Too many open files                                     }
  357.    {                5 File access denied                                      }
  358.    {              102 File not assigned                                       }
  359.    {    ReWrite :   3 Path not found                                          }
  360.    {                4 Too many open files                                     }
  361.    {                5 File access denied                                      }
  362.    {              102 File not assigned                                       }
  363.    {    Close   : 101 Disk write error                                        }
  364.    {              103 File not open                                           }
  365.    {--------------------------------------------------------------------------}
  366.       Var
  367.          Buf        : Array[1..2048] Of Char;
  368.          CopyName   : FileNameType;
  369.          FFrom      : File;                                    { Untyped file }
  370.          FTo        : File;                                    { Untyped file }
  371.          NumRead    : Word;
  372.          NumWritten : Word;
  373.  
  374.       Begin
  375.          {--------------------------------------------------------------------}
  376.          { Close file 'F' and reopen as an untyped input file                 }
  377.          {--------------------------------------------------------------------}
  378.          CClose;
  379.          Assign(FFrom, FileName);
  380.          System.Reset(FFrom);
  381.  
  382.          Case IOResult Of
  383.             2 : Begin                                        { File not found }
  384.                System.ReWrite(F);
  385.                Reset;
  386.             End; {2}
  387.  
  388.             3 : Begin                                        { Path not found }
  389.                TextOK := False;
  390.                TextError := 3;
  391.             End; {3}
  392.  
  393.             4 : Begin                                   { Too many open files }
  394.                TextOK := False;
  395.                TextError := 4;
  396.             End; {4}
  397.  
  398.             5 : Begin                                    { File access denied }
  399.                TextOK := False;
  400.                TextError := 5;
  401.             End; {5}
  402.  
  403.             102 : Begin                                   { File not assigned }
  404.                TextOK := False;                   { This should never happen! }
  405.                TextError := 102;
  406.             End; {102}
  407.          End; {Case}
  408.  
  409.          {--------------------------------------------------------------------}
  410.          { Allocate memory for CopyName and open for untyped output           }
  411.          {--------------------------------------------------------------------}
  412.          GetMem(CopyName, fsPathName+1);                  { Allow room for #0 }
  413.          FileExpand(CopyName, DestName);
  414.          Assign(FTo, CopyName);
  415.          System.ReWrite(FTo);
  416.  
  417.          Case IOResult Of
  418.             3 : Begin                                        { Path not found }
  419.                TextOK := False;
  420.                TextError := 3;
  421.             End; {3}
  422.  
  423.             4 : Begin                                   { Too many open files }
  424.                TextOK := False;
  425.                TextError := 4;
  426.             End; {4}
  427.  
  428.             5 : Begin                                    { File access denied }
  429.                TextOK := False;
  430.                TextError := 5;
  431.             End; {5}
  432.  
  433.             102 : Begin                                   { File not assigned }
  434.                TextOK := False;                   { This should never happen! }
  435.                TextError := 102;
  436.             End; {102}
  437.          End; {Case}
  438.  
  439.          {--------------------------------------------------------------------}
  440.          { Proveded we have no errors, copy contents from the untyped source  }
  441.          { file to the untyped target file                                    }
  442.          {--------------------------------------------------------------------}
  443.          If (TextOK) Then
  444.             Repeat
  445.                BlockRead(FFrom, Buf, SizeOf(Buf), NumRead);
  446.                BlockWrite(FTo, Buf, NumRead, NumWritten);
  447.             Until (NumRead = 0) or (NumWritten <> NumRead);
  448.          {EndIf}
  449.  
  450.          {--------------------------------------------------------------------}
  451.          { Close both the untyped source and untyped target files             }
  452.          {--------------------------------------------------------------------}
  453.          System.Close(FFrom);
  454.          Case IOResult Of
  455.             101 : Begin                                    { Disk write error }
  456.                TextOK := False;
  457.                TextError := 101;
  458.             End; {101}
  459.  
  460.             103 : { We don't worry about a 103 - obviously! }; { File not open }
  461.          End; {Case}
  462.  
  463.          System.Close(FTo);
  464.          Case IOResult Of
  465.             101 : Begin                                    { Disk write error }
  466.                TextOK := False;
  467.                TextError := 101;
  468.             End; {101}
  469.  
  470.             103 : { We don't worry about a 103 - obviously! }; { File not open }
  471.          End; {Case}
  472.  
  473.          {--------------------------------------------------------------------}
  474.          { Dispose dynamic memory allocated for target file name              }
  475.          {--------------------------------------------------------------------}
  476.          If (CopyName <> nil) Then                   { Dispose dynamic memory }
  477.             FreeMem(CopyName, fsPathName+1);
  478.          {EndIf}
  479.  
  480.          {--------------------------------------------------------------------}
  481.          { Reopen file 'F' in the state it maintained before FCopy            }
  482.          {--------------------------------------------------------------------}
  483.          Case FileMode Of
  484.             NS_Reset   : Reset;
  485.             NS_ReWrite : ReWrite;
  486.             NS_Append  : Append;
  487.          End; {Case}
  488.       End;
  489.    {EndProcedure}
  490.  
  491.    Function TTextFile.EOF;
  492.    {--------------------------------------------------------------------------}
  493.    { Error information:                                                       }
  494.    {    EOF     : 103 File not open                                           }
  495.    {              104 File not open for input                                 }
  496.    {--------------------------------------------------------------------------}
  497.       Begin
  498.          EOF := System.EOF(F);
  499.          Case IOResult Of
  500.             103 : Begin                                       { File not open }
  501.                TextOK := False;
  502.                TextError := 103;
  503.             End; {103}
  504.  
  505.             104 : Begin                             { File not open for input }
  506.                TextOK := False;
  507.                TextError := 104;
  508.             End; {104}
  509.          End; {Case}
  510.       End;
  511.    {EndFunction}
  512.  
  513.    Function TTextFile.EOLN;
  514.    {--------------------------------------------------------------------------}
  515.    { Error information:                                                       }
  516.    {    EOLN    : 104 File not open for input                                 }
  517.    {--------------------------------------------------------------------------}
  518.       Begin
  519.          EOLN := System.EOLN(F);
  520.          Case IOResult Of
  521.             104 : Begin                             { File not open for input }
  522.                TextOK := False;
  523.                TextError := 104;
  524.             End; {104}
  525.          End; {Case}
  526.       End;
  527.    {EndFunction}
  528.  
  529.    Function TTextFile.SeekEOF;
  530.    {--------------------------------------------------------------------------}
  531.    { Error information:                                                       }
  532.    {    SeekEOF : 104 File not open for input                                 }
  533.    {--------------------------------------------------------------------------}
  534.       Begin
  535.          SeekEOF := System.SeekEOF(F);
  536.          Case IOResult Of
  537.             104 : Begin                             { File not open for input }
  538.                TextOK := False;
  539.                TextError := 104;
  540.             End; {104}
  541.          End; {Case}
  542.       End;
  543.    {EndFunction}
  544.  
  545.    Function TTextFile.SeekEOLN;
  546.    {--------------------------------------------------------------------------}
  547.    { Error information:                                                       }
  548.    {    SeekEOLN: 104 File not open for input                                 }
  549.    {--------------------------------------------------------------------------}
  550.       Begin
  551.          SeekEOLN := System.SeekEOLN(F);
  552.          Case IOResult Of
  553.             104 : Begin                             { File not open for input }
  554.                TextOK := False;
  555.                TextError := 104;
  556.             End; {104}
  557.          End; {Case}
  558.       End;
  559.    {EndFunction}
  560.  
  561.    Procedure TTextFile.Read;
  562.    {--------------------------------------------------------------------------}
  563.    { Extensible? YES - Allows for further definition of input variables       }
  564.    { Error information:                                                       }
  565.    {    Read    : 103 File not open                                           }
  566.    {              104 File not open for input                                 }
  567.    {              106 Invalid numeric format                                  }
  568.    {--------------------------------------------------------------------------}
  569.       Begin
  570.          System.Read(F, Item);
  571.          Case IOResult Of
  572.             103 : Begin                                       { File not open }
  573.                TextOK := False;
  574.                TextError := 103;
  575.             End; {103}
  576.  
  577.             104 : Begin                             { File not open for input }
  578.                TextOK := False;
  579.                TextError := 104;
  580.             End; {104}
  581.  
  582.             106 : Begin                              { Invalid numeric format }
  583.                TextOK := False;
  584.                TextError := 106;
  585.             End; {106}
  586.          End; {Case}
  587.       End;
  588.    {EndProcedure}
  589.  
  590.    Procedure TTextFile.ReadLn;
  591.    {--------------------------------------------------------------------------}
  592.    { Extensible? YES - Allows for further definition of input variables       }
  593.    { Error information:                                                       }
  594.    {    ReadLn  : 104 File not open for input                                 }
  595.    {              106 Invalid numeric format                                  }
  596.    {--------------------------------------------------------------------------}
  597.       Begin
  598.          System.ReadLn(F, Item);
  599.          Case IOResult Of
  600.             104 : Begin                             { File not open for input }
  601.                TextOK := False;
  602.                TextError := 104;
  603.             End; {104}
  604.  
  605.             106 : Begin                              { Invalid numeric format }
  606.                TextOK := False;
  607.                TextError := 106;
  608.             End; {106}
  609.          End; {Case}
  610.       End;
  611.    {EndProcedure}
  612.  
  613.    Procedure TTextFile.Write;
  614.    {--------------------------------------------------------------------------}
  615.    { Extensible? YES - Allows for further definition of output variables.     }
  616.    {    BE SURE TO CALL SetFlushBuffer!                                       }
  617.    { Error information:                                                       }
  618.    {    Write   : 101 Disk write error                                        }
  619.    {              103 File not open                                           }
  620.    {              105 File not open for output                                }
  621.    {--------------------------------------------------------------------------}
  622.       Begin
  623.          System.Write(F, Item);
  624.          Case IOResult Of
  625.             101 : Begin                                       { File not open }
  626.                TextOK := False;
  627.                TextError := 101;
  628.             End; {101}
  629.  
  630.             103 : Begin                                       { File not open }
  631.                TextOK := False;
  632.                TextError := 103;
  633.             End; {103}
  634.  
  635.             105 : Begin                            { File not open for output }
  636.                TextOK := False;
  637.                TextError := 105;
  638.             End; {105}
  639.          Else
  640.             If (FileInteg = NS_High) Then
  641.                Flush
  642.             Else
  643.                FlushBuffer := True;                   { Data buffered to disk }
  644.             {EndIf}
  645.          End; {Case}
  646.       End;
  647.    {EndProcedure}
  648.  
  649.    Procedure TTextFile.WriteLn;
  650.    {--------------------------------------------------------------------------}
  651.    { Extensible? YES - Allows for further definition of output variables.     }
  652.    {    BE SURE TO CALL SetFlushBuffer!                                       }
  653.    { Error information:                                                       }
  654.    {    WriteLn : 101 Disk write error                                        }
  655.    {              105 File not open for output                                }
  656.    {--------------------------------------------------------------------------}
  657.       Begin
  658.          FlushBuffer := True;                         { Data buffered to disk }
  659.          System.WriteLn(F, Item);
  660.          Case IOResult Of
  661.             101 : Begin                                       { File not open }
  662.                TextOK := False;
  663.                TextError := 101;
  664.             End; {101}
  665.  
  666.             105 : Begin                            { File not open for output }
  667.                TextOK := False;
  668.                TextError := 105;
  669.             End; {105}
  670.          Else
  671.             If (FileInteg = NS_High) Then
  672.                Flush
  673.             Else
  674.                FlushBuffer := True;                   { Data buffered to disk }
  675.             {EndIf}
  676.          End; {Case}
  677.       End;
  678.    {EndProcedure}
  679.  
  680.    Procedure TTextFile.GetFTime;
  681.    {--------------------------------------------------------------------------}
  682.    { Error information:                                                       }
  683.    {    GetFTime   : 6 Invalid file handle                                    }
  684.    {--------------------------------------------------------------------------}
  685.       Begin
  686.          If (FileMode = NS_Closed) Then
  687.             Begin
  688.                Reset;
  689.                GetFTime(Time);
  690.                CClose;
  691.             End
  692.          Else
  693.             Begin
  694.                WinDOS.GetFTime(F, Time);
  695.                Case IOResult Of
  696.                   6 : Begin                             { Invalid file handle }
  697.                      TextOK := False;
  698.                      TextError := 6;
  699.                   End; {6}
  700.                End; {Case}
  701.             End;
  702.          {EndIf}
  703.       End;
  704.    {EndProcedure}
  705.  
  706.    Procedure TTextFile.SetFTime;
  707.    {--------------------------------------------------------------------------}
  708.    { Error information:                                                       }
  709.    {    SetFTime : 6 Invalid file handle                                     }
  710.    {--------------------------------------------------------------------------}
  711.       Begin
  712.          If (FileMode = NS_Closed) Then
  713.             Begin
  714.                Reset;
  715.                SetFTime(Time);
  716.                CClose;
  717.             End
  718.          Else
  719.             Begin
  720.                WinDOS.SetFTime(F, Time);
  721.                Case IOResult Of
  722.                   6 : Begin                             { Invalid file handle }
  723.                      TextOK := False;
  724.                      TextError := 6;
  725.                   End; {6}
  726.                End; {Case}
  727.             End;
  728.          {EndIf}
  729.       End;
  730.    {EndProcedure}
  731.  
  732.    Function TTextFile.FileSplit;
  733.    {--------------------------------------------------------------------------}
  734.       Begin
  735.          FileSplit := WinDOS.FileSplit(FileName, Dir, Name, Ext);
  736.       End;
  737.    {EndFunction}
  738.  
  739.    Procedure TTextFile.GetFAttr;
  740.    {--------------------------------------------------------------------------}
  741.    { Error information:                                                       }
  742.    {    GetFAttr   : 3 Invalid path                                          }
  743.    {                 5 File access denied                                     }
  744.    {--------------------------------------------------------------------------}
  745.       Begin
  746.          If (FileMode <> NS_Closed) Then
  747.             Begin
  748.                CClose;
  749.                GetFAttr(Attr);
  750.  
  751.                Case FileMode Of
  752.                   NS_Reset   : Reset;
  753.                   NS_ReWrite : ReWrite;
  754.                   NS_Append  : Append;
  755.                End; {Case}
  756.             End
  757.          Else
  758.             Begin
  759.                WinDOS.GetFAttr(F, Attr);
  760.                Case IOResult Of
  761.                   3 : Begin                                    { Invalid path }
  762.                      TextOK := False;
  763.                      TextError := 3;
  764.                   End; {3}
  765.  
  766.                   5 : Begin                              { File access denied }
  767.                      TextOK := False;
  768.                      TextError := 5;
  769.                   End; {5}
  770.                End; {Case}
  771.             End;
  772.          {EndIf}
  773.       End;
  774.    {EndProcedure}
  775.  
  776.    Procedure TTextFile.SetFAttr;
  777.    {--------------------------------------------------------------------------}
  778.    { Error information:                                                       }
  779.    {    SetFAttr : 3 Invalid path                                           }
  780.    {               5 File access denied                                       }
  781.    {--------------------------------------------------------------------------}
  782.       Begin
  783.          If (FileMode <> NS_Closed) Then
  784.             Begin
  785.                CClose;
  786.                SetFAttr(Attr);
  787.  
  788.                Case FileMode Of
  789.                   NS_Reset   : Reset;
  790.                   NS_ReWrite : ReWrite;
  791.                   NS_Append  : Append;
  792.                End; {Case}
  793.             End
  794.          Else
  795.             Begin
  796.                WinDOS.SetFAttr(F, Attr);
  797.                Case IOResult Of
  798.                   3 : Begin                                    { Invalid path }
  799.                      TextOK := False;
  800.                      TextError := 3;
  801.                   End; {3}
  802.  
  803.                   5 : Begin                              { File access denied }
  804.                      TextOK := False;
  805.                      TextError := 5;
  806.                   End; {5}
  807.                End; {Case}
  808.             End;
  809.          {EndIf}
  810.       End;
  811.    {EndProcedure}
  812.  
  813.    Destructor TTextFile.Close;
  814.    {--------------------------------------------------------------------------}
  815.    { Error information:                                                       }
  816.    {    Close   : 101 Disk write error                                        }
  817.    {              103 File not open                                           }
  818.    {--------------------------------------------------------------------------}
  819.       Begin
  820.          CClose;
  821.  
  822.          If (FileName <> nil) Then { Dispose dynamic memory allocated in Init }
  823.             FreeMem(FileName, fsPathName+1);
  824.          {EndIf}
  825.       End;
  826.    {EndFunction}
  827.  
  828.    Destructor TTextFile.Erase;
  829.    {--------------------------------------------------------------------------}
  830.    { Error information:                                                       }
  831.    {    Close   : 101 Disk write error                                        }
  832.    {              103 File not open                                           }
  833.    {    Erase   :   2 File not found                                          }
  834.    {                3 Path not found                                          }
  835.    {                5 File access denied                                      }
  836.    {              102 File not assigned                                       }
  837.    {--------------------------------------------------------------------------}
  838.       Begin
  839.          CClose;               { Erase must NEVER be used on an open file! }
  840.  
  841.          If (TextOK) Then
  842.             Begin
  843.                System.Erase(F);
  844.                Case IOResult Of
  845.                   2 : Begin
  846.                      TextOK := False;
  847.                      TextError := 2;
  848.                   End; {2}
  849.  
  850.                   3 : Begin
  851.                      TextOK := False;
  852.                      TextError := 3;
  853.                   End; {3}
  854.  
  855.                   5 : Begin
  856.                      TextOK := False;
  857.                      TextError := 5;
  858.                   End; {5}
  859.  
  860.                   102 : Begin
  861.                      TextOK := False;
  862.                      TextError := 102;
  863.                   End; {102}
  864.                End; {Case}
  865.             End;
  866.          {EndIf}
  867.  
  868.          If (FileName <> nil) Then   { Dispose dynamic memory allocated in Init }
  869.             FreeMem(FileName, fsPathName+1);
  870.          {EndIf}
  871.       End;
  872.    {EndProcedure}
  873.  
  874.    Procedure TTextFile.CClose;
  875.    {--------------------------------------------------------------------------}
  876.    { Error information:                                                       }
  877.    {    Close   : 101 Disk write error                                      }
  878.    {              103 File not open                                           }
  879.    {--------------------------------------------------------------------------}
  880.       Begin
  881.          System.Close(F);
  882.          Case IOResult Of
  883.             101 : Begin                                    { Disk write error }
  884.                TextOK := False;
  885.                TextError := 101;
  886.             End; {101}
  887.  
  888.             103 : { We don't worry about a 103 - obviously! }; { File not open }
  889.          End; {Case}
  890.       End;
  891.    {EndFunction}
  892. End.
  893.  
  894. {$F-}
  895.